package com.nbcb.fund.common.exception.handler;

import com.nbcb.fund.common.exception.exception.BusinessException;
import com.nbcb.fund.entity.code.FundCode;
import com.nbcb.fund.entity.dto.FundResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author linqiankun
 */
@Slf4j
@RestControllerAdvice
public class BusinessExceptionHandler {


    /**
     * 自定义业务异常，数据校验，业务逻辑异常走这里
     *
     * @param e 异常
     * @return FundResult
     */
    @ExceptionHandler({BusinessException.class})
    public FundResult processException(BusinessException e) {
        FundCode eBwcrmCode = e.getFundCode();
        if (eBwcrmCode != null) {
            log.error("业务异常,业务信息:{},异常信息:{}", eBwcrmCode.getMessage(), e);
            return FundResult.bizfail(eBwcrmCode);
        } else if (e.getMsg() != null) {
            log.error("业务异常,业务信息:{},异常信息:{}", e.getMsg(), e);
            return FundResult.bizfail(e.getMsg());
        } else {
            log.error("业务异常,异常信息:{}", e);
            return FundResult.bizfail(FundCode.BIZFAIL);
        }

    }

//    @ExceptionHandler({MethodArgumentNotValidException.class})
//    public FundResult processException(MethodArgumentNotValidException e) {
//        log.error("参数校验异常:{}", e);
//        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
//        StringBuilder errorMessages = new StringBuilder();
//        for (FieldError f : fieldErrors) {
//            errorMessages.append(f.getDefaultMessage());
//        }
//        log.error("参数校验异常,参数信息:{},异常信息:{}", errorMessages.toString(), e);
//        return FundResult.fail();
//    }

    @ExceptionHandler({Exception.class})
    public FundResult processException(Exception e) {
        log.error("系统异常:{}", e);
        return FundResult.fail();
    }


}
